home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / ether.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  7.3 KB  |  290 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: ether.c,v 4.2 88/06/22 14:37:31 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/ether.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/ether.c,v $$Revision: 4.2 $";
  12.  
  13. /* strip_chart vmstat output version II */
  14.  
  15. #include "term.h"
  16. #include <signal.h>
  17. #include <sgtty.h>
  18.  
  19. FILE *popen(), *file;
  20.  
  21. #define INTERVAL    60    /* bar interval (in secs) */
  22. #define SCROLL        4    /* # of scrolls per window */
  23. #define MAX        3    /* max number of plots */
  24. #define FREQ        3    /* update frequency (secs)*/
  25. #define DELTA        4    /* pixels/update */
  26.  
  27. #define Min(x,y)    ((x)>(y)?(y):(x))
  28. #define Max(x,y)    ((x)>(y)?(x):(y))
  29. #define dprintf        if (debug) fprintf
  30.  
  31. char *labels[] = {        /* graph labels */
  32.    "Input" , "Output", "Coll."
  33.    };
  34.  
  35. int indx[] = {            /* field indeces into netstat(1) */
  36.    0, 2, 4
  37.    };
  38.  
  39. int limit[] = {            /* default maximums */
  40.    15, 15, 3
  41.    };
  42.  
  43. main(argc,argv)
  44. int argc;
  45. char **argv;
  46.    {
  47.    register int i;
  48.    register int x;    /* current plot position */
  49.    int bar=0;        /* hash mark counter */
  50.    int back=0;        /* enable background writes */
  51.    int solid=1;        /* make solid lines */
  52.    int freq = FREQ;    /* update frequency (secs) */
  53.    int size;        /* size of label region */
  54.    int interval;    /* hash mark interval (secs) */
  55.  
  56.    char host[16];    /* hostname */
  57.    char title[255];    /* chart title */
  58.    char line[255];    /* vmstat input buffer */
  59.    char *fields[20];    /* place for raw input fields */
  60.  
  61.    int current[MAX];    /* current data value */
  62.    int old[MAX];    /* previous data value */
  63.    int first =1;    /* first time through */ 
  64.    int clean();
  65.  
  66.    int f_high, f_wide;        /* font size */
  67.    int high,wide;        /* window size */
  68.    int x1;            /* left margin */
  69.    int x2;            /* title */
  70.    int y1;            /* title line */
  71.    int y2;            /* first bar */
  72.    int scroll;            /* scroll amount */
  73.    int delta=DELTA;        /* pixels/line */
  74.    int item;
  75.    int dummy;
  76.    int debug=0;
  77.  
  78.    ckmgrterm( *argv );
  79.  
  80.    for(i=1;i<argc;i++) {
  81.       if (strcmp(argv[i],"-d")==0) {
  82.          debug++;
  83.          }
  84.       else if (strncmp(argv[i],"-f",2)==0) {
  85.          freq = atoi(argv[i]+2);
  86.          if (freq < 1) freq = 1;
  87.          if (freq > 120) freq = 120;
  88.          }
  89.       else if (strncmp(argv[i],"-m",2)==0) {
  90.          int max;
  91.          max = atoi(argv[i]+2);
  92.          if (max < 1) max = 1;
  93.          if (max > 999) max = 999;
  94.          limit[0] = limit[1] = max;
  95.          limit[2] = (max>=5) ? max/5 : 1;
  96.          
  97.          }
  98.       else
  99.          fprintf(stderr,"%s: unknown flag %s. Ignored\n",*argv,argv[i]);
  100.       }
  101.  
  102.    sprintf(line,"netstat %d",freq);
  103.    if ((file = popen(line,"r")) == NULL)
  104.       exit(1);
  105.  
  106.    m_setup(M_FLUSH);
  107.    m_ttyset();
  108.    m_push(P_EVENT|P_FLAGS);
  109.    m_setmode(M_ABS);
  110.  
  111.    signal(SIGINT,clean);
  112.    signal(SIGTERM,clean);
  113.  
  114.    system("stty -ctlecho");
  115.    m_setevent(RESHAPE,"R\fRedrawing...\n");
  116.    m_setevent(REDRAW,"R\fRedrawing...\n");
  117.    first = 1;
  118.  
  119.    while (1) {
  120.  
  121.       for(size=0,i=0;i<MAX;i++)
  122.          size = Max(size,strlen(labels[i]));
  123.  
  124.       /* clear the screen, flush pending input */
  125.  
  126.       read_it(fileno(m_termin),line);
  127.  
  128.       /* get font size */
  129.  
  130.       get_font(&f_wide,&f_high);
  131.  
  132.       /* get window size */
  133.  
  134.       get_size(&dummy,&dummy,&wide,&high);
  135.    
  136.       if (wide==0 || high==0 || f_wide==0 || f_high==0) {
  137.          fprintf(stderr,"Can't get window info\n");
  138.          clean();
  139.          }
  140.  
  141.       /* get the title */
  142.  
  143.       gethostname(host,sizeof(host));
  144.  
  145.       sprintf(title,"Network statistics for %s in packets/%d seconds",
  146.               host,freq);
  147.  
  148.       if (strlen(title)*f_wide > wide)
  149.          sprintf(title,"%s (pkts/%ds.)",host,freq);
  150.  
  151.       /* make sure window is big enough */
  152.  
  153.       if (f_high * (2*MAX+1) > high) {
  154.          fprintf(stderr,"\fWindow isn't tall enough\n");
  155.          m_gets(line);
  156.          continue;
  157.          }
  158.       if (f_high * (3*MAX +1) > high)
  159.          size += 3;
  160.  
  161.       if (strlen(title)*f_wide > wide || 3*size*f_wide > wide*2) {
  162.          fprintf(stderr,"\fWindow isn't\nwide enough\n");
  163.          m_gets(line);
  164.          continue;
  165.          }
  166.  
  167.       /* calculate key positions */
  168.  
  169.       x1 = (size*f_wide+1);
  170.       x2 = (wide-strlen(title)*f_wide)/2;
  171.       y1 = f_high +1;
  172.       y2 = (high - y1) /MAX;
  173.       high--;
  174.  
  175.       m_func(B_SET);
  176.       m_clear();
  177.       x = x1;
  178.       scroll = Max((wide-x1)/SCROLL,10);
  179.       scroll += scroll%delta;
  180.  
  181.       if (freq >15)
  182.          interval = INTERVAL * 10 /freq;
  183.       else 
  184.          interval = INTERVAL / freq;
  185.  
  186.       /* draw form */
  187.  
  188.       m_moveprint(x2,y1,title);
  189.  
  190.       for(i=0;i<MAX;i++) {
  191.          m_moveprint(x1-f_wide,high-i*y2,"0");
  192.          sprintf(line,"%3d",limit[i]);
  193.          m_moveprint(x1-f_wide*3,high-(i+1)*y2+f_wide*2+1,line);
  194.          m_moveprint(1,high-i*y2-(y2-f_high)/2,labels[i]);
  195.          m_line(x1,high-i*y2,wide,high-i*y2);
  196.          }
  197.    
  198.       m_line(0,y1,wide,y1);
  199.       m_line(x1,y1,x1,high);
  200.       m_movecursor(x1,0);
  201.       m_flush();
  202.  
  203.       /* read the data */
  204.  
  205.       while (fgets(line,sizeof(line),file) != NULL) {
  206.          i = parse(line,fields);
  207.          if (strcmp(*fields,"input")==0) {
  208.             fgets(line,sizeof(line),file);
  209.             fgets(line,sizeof(line),file);
  210.             continue;
  211.             }
  212.  
  213.          /* calculate new line position */
  214.  
  215.          for(i=0;i<MAX;i++) {
  216.             current[i] = atoi(fields[indx[i]]) *
  217.                          (y2-3)/limit[i];
  218.             current[i] = Min(current[i],y2-3) + y2*i + 1;
  219.  
  220.             if (!first) {
  221.                m_line(x,high-old[i],x+delta,high-current[i]);
  222.                if (solid)
  223.                   m_line(x+delta,high-y2*i,x+delta,high-current[i]);
  224.                }
  225.  
  226.             dprintf(stderr,"%s %d->%d, ",labels[i],
  227.                             high-old[i],high-current[i]);
  228.             old[i] = current[i];
  229.             }
  230.          dprintf(stderr," [%d]\n",high);
  231.    
  232.          if (++bar  == interval) {
  233.             m_line(x,y1,x,high);
  234.             bar = 0;
  235.             dprintf(stderr,"---------\n");
  236.             }
  237.  
  238.          if (first)
  239.             first = 0;
  240.          else
  241.             x += delta;
  242.  
  243.          if (x > wide-delta) {
  244.  
  245.             /* scroll the display */
  246.  
  247.             x -= scroll;
  248.             m_func(B_COPY);
  249.             m_bitcopy(x1+1,y1+1,wide-x1-1,high-y1-1,x1+scroll+1,y1+1);
  250.             m_func(B_CLEAR);
  251.             m_bitwrite(wide-scroll,y1+1,scroll,high);
  252.             m_func(B_SET);
  253.          
  254.             dprintf(stderr,"scroll to %d,%d from %d,%d\n",
  255.                     x1+1,y1+1,x1+scroll+1,y1+1);
  256.             for(i=0;i<MAX;i++) 
  257.                m_line(wide-scroll,high-i*y2,wide,high-i*y2);
  258.             }
  259.          m_flush();
  260.          if (read_it(fileno(m_termin),line) && *line == 'R')
  261.             break;
  262.          }
  263.       }
  264.    }
  265.  
  266. clean()            /* clean up on SIGINT */
  267.    {
  268.    m_pop();
  269.    pclose(file);
  270.    m_clear();
  271.    m_flush();
  272.    m_ttyreset();
  273.    exit(1);
  274.    }
  275.  
  276. int read_it(fd,line)    /* non blocking read */
  277. int fd;
  278. char *line;
  279.    {
  280.    long rd;
  281.  
  282.    ioctl(fd,FIONREAD,&rd);
  283.    line[rd] = '\0';
  284.    if (rd > 0)  {
  285.       return(read(fd,line,rd));
  286.       }
  287.    else
  288.       return(0);
  289.    }
  290.